package com.hyx.api.service.impl.pc_admin;

import java.util.List;
import java.util.Arrays;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.hyx.api.service.pc_admin.IRecycleReceivingAddrService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.hyx.api.mapper.RecycleReceivingAddrMapper;
import com.hyx.api.domain.RecycleReceivingAddr;
import com.hyx.api.domain.bo.RecycleReceivingAddrBo;
import com.hyx.api.domain.vo.RecycleReceivingAddrVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;

/**
 * 回收商收货地址Service业务层处理
 *
 * @author HK
 * @date 2023-11-18
 */
@RequiredArgsConstructor
@Service
public class RecycleReceivingAddrServiceImpl implements IRecycleReceivingAddrService
{
    private final RecycleReceivingAddrMapper recycleReceivingAddrMapper;

    /**
     * 查询回收商收货地址
     *
     * @param id 回收商收货地址主键
     * @return 回收商收货地址
     */
    @Override
    public RecycleReceivingAddrVo selectRecycleReceivingAddrById(Long id)
    {
        //TODO 表以外的字段，写入vo
        return recycleReceivingAddrMapper.selectVoById(id);
    }

    /**
     * 查询回收商收货地址列表
     *
     * @param bo 回收商收货地址
     * @return 回收商收货地址
     */
    @Override
    public List<RecycleReceivingAddrVo> selectRecycleReceivingAddrList(RecycleReceivingAddrBo bo)
    {
        LambdaQueryWrapper<RecycleReceivingAddr> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjectUtils.isNotEmpty(bo.getRecycleBusId()), RecycleReceivingAddr::getRecycleBusId, bo.getRecycleBusId());
        lqw.like(ObjectUtils.isNotEmpty(bo.getContactName()), RecycleReceivingAddr::getContactName, bo.getContactName());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getContactMobile()), RecycleReceivingAddr::getContactMobile, bo.getContactMobile());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getProvinceId()), RecycleReceivingAddr::getProvinceId, bo.getProvinceId());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getCityId()), RecycleReceivingAddr::getCityId, bo.getCityId());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getRegionId()), RecycleReceivingAddr::getRegionId, bo.getRegionId());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getDetailAddr()), RecycleReceivingAddr::getDetailAddr, bo.getDetailAddr());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getFullAddr()), RecycleReceivingAddr::getFullAddr, bo.getFullAddr());
        return recycleReceivingAddrMapper.selectVoList(lqw);
    }

    /**
     * 新增回收商收货地址
     *
     * @param bo 回收商收货地址
     * @return 结果
     */
    @Override
    public int insertRecycleReceivingAddr(RecycleReceivingAddrBo bo)
    {
        RecycleReceivingAddr recycleReceivingAddr = tranEntityBeforeSave(bo);
        validEntityBeforeSave(recycleReceivingAddr);
        return recycleReceivingAddrMapper.insert(recycleReceivingAddr);
    }

    /**
     * 修改回收商收货地址
     *
     * @param bo 回收商收货地址
     * @return 结果
     */
    @Override
    public int updateRecycleReceivingAddr(RecycleReceivingAddrBo bo)
    {
        RecycleReceivingAddr recycleReceivingAddr = tranEntityBeforeSave(bo);
        return recycleReceivingAddrMapper.updateById(recycleReceivingAddr);
    }

    /**
     * 批量删除回收商收货地址
     *
     * @param ids 需要删除的回收商收货地址主键
     * @return 结果
     */
    @Override
    public int deleteRecycleReceivingAddrByIds(Long[] ids)
    {
        return recycleReceivingAddrMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(RecycleReceivingAddr entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     *   保存前的数据转换
     */
    private RecycleReceivingAddr tranEntityBeforeSave(RecycleReceivingAddrBo bo) {
        //TODO 做一些数据转换
        RecycleReceivingAddr recycleReceivingAddr = new RecycleReceivingAddr();
        BeanUtils.copyProperties(bo, recycleReceivingAddr);
        return recycleReceivingAddr;
    }
}

